//
//  AppDelegate.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/10.
//  Copyright © 2017年 优房网. All rights reserved.
//

////////////////////////////////////////////////////////////////////
//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//         佛祖保佑       永无BUG     永不修改                        //
////////////////////////////////////////////////////////////////////

#import "AppDelegate.h"
#import "SISSLoginViewController.h"
#import "SISSTabBarController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BMKLocationKit/BMKLocationAuth.h>
#import "SISSJsonRequestNetworkingManager+Login.h"
#import "AppDelegate+Chat.h"
#import "AppDelegate+Push.h"
#import "AppDelegate+Share.h"
#import <SAMKeychain/SAMKeychain.h>
#import "SISSLoginManger.h"
#import <UMAnalytics/MobClick.h>

#import <SDWebImage/UIImageView+WebCache.h>
@interface AppDelegate ()<BMKLocationAuthDelegate>

@property (strong, nonatomic) UIView *lunchView;

@end

@implementation AppDelegate {
     BMKMapManager * _mapManager;
}
@synthesize lunchView;
- (void)removeLaunch {
    [lunchView removeFromSuperview];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
#ifdef DEBUG
//    [self printBuddhaBless];
#else
    
#endif

    NSLog(@"%@",BASE_URL);
    
    // 初始化
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    UIImage *image = [PublicUtil imageWithColor:[UIColor blackColor] size:CGSizeMake(512, 512)];
//    NSString *filePath = [@"/Users/yuanyingrong/Pictures" stringByAppendingPathComponent:@"black.png"];
//    BOOL result = [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
//    if (result == YES) {
//        NSLog(@"保存成功");
//    }
    // 设置跟控制器
    self.window.rootViewController = [[SISSLoginViewController alloc] init];
    if ([SISSTools currentLoginState]) {
        NSString *password = [SAMKeychain passwordForService:BUNDLE_ID account:[SISSUserAccount shared].userName] ? [SAMKeychain passwordForService:BUNDLE_ID account:[SISSUserAccount shared].userName] : @"";
        [[SISSLoginManger shareInstance] loginWithUserName:[SISSUserAccount shared].userName password:password];
    }
    [self.window makeKeyAndVisible];
    
    // 设置键盘
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
#pragma mark - 环信
    [self configChat];
    
#pragma mark - 友盟 
    // 分享
    [self configShare];
    
    // 统计
    //设置为自动采集页面
    [MobClick setScenarioType:E_UM_NORMAL];
    [MobClick setAutoPageEnabled:YES];
//    [MobClick setCrashReportEnabled:NO];
    
#pragma mark - 极光推送
    [self configPushWithLaunchOptions:launchOptions];
    [JPUSHService setBadge:0];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

#pragma mark - 监听网络状态
    [self networkSniffering];
#pragma mark - 百度地图
    // 初始化定位SDK
    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:BMKMAP_KEY authDelegate:self];
    // 要使用百度地图，请先启动BaiduMapManager
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [_mapManager start:BMKMAP_KEY generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    
    // 解决签名后显示的宽度问题
    [SISSPublicSingleton shared].screenWidth = SCREEN_WIDTH;
    
//#ifdef DEBUG
//    // 打开调试输出
//    [[GHConsole sharedConsole] startPrintLog];
//#else
//    
//#endif
    lunchView = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil] instantiateInitialViewController].view;
    lunchView.frame = CGRectMake(0, 0, self.window.screen.bounds.size.width, self.window.screen.bounds.size.height);
    [self.window addSubview:lunchView];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    NSString *str = @"";//@"http://www.jerehedu.com/images/temp/logo.gif";
    [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"defaultLaunchImage"]];
    [lunchView addSubview:imageV];
    
    [self.window bringSubviewToFront:lunchView];
    
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(removeLaunch) userInfo:nil repeats:NO];
    
    
    return YES;
}


- (void)networkSniffering {
    
    // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        /*
         AFNetworkReachabilityStatusUnknown          = -1,
         AFNetworkReachabilityStatusNotReachable     = 0,
         AFNetworkReachabilityStatusReachableViaWWAN = 1,
         AFNetworkReachabilityStatusReachableViaWiFi = 2,
         */
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"网络状态未知");
                [SISSPublicSingleton shared].isNoNetwork = NO;
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"没有网络");
                [SISSPublicSingleton shared].isNoNetwork = YES;
                break;
            case  AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"3G|4G蜂窝移动网络");
                [SISSPublicSingleton shared].isNoNetwork = NO;
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WIFI网络");
                [SISSPublicSingleton shared].isNoNetwork = NO;
                break;
            default:
                break;
        }
    }];
}

// 1.当设备横竖屏转换的时候回调用这个方法
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    //为1的话,说明某个界面需要横竖屏转化
    if (_Rotate == 1) {
        return UIInterfaceOrientationMaskAll;
    }else {
        //为0的话,说明只需要竖屏
        return (UIInterfaceOrientationMaskPortrait); }
}

// 2.是否支持设备自动旋转
- (BOOL)shouldAutorotate {
    if (_Rotate == 1) {
        //为1的话,支持旋转
        return YES;
    }
    return NO;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
}

- (void)printBuddhaBless {
    NSLog(@"////////////////////////////////////////////////////////////////////\n//                          _ooOoo_                               //\n//                         o8888888o                              //\n//                         88\" . \"88                              //\n//                         (| ^_^ |)                              //\n//                         O\\  =  /O                              //\n//                      ____/`---'\\____                           //\n//                    .'  \\\\|     |//  `.                         //\n//                   /  \\\\|||  :  |||//  \\                        //\n//                  /  _||||| -:- |||||-  \\                       //\n//                  |   | \\\\\\  -  /// |   |                       //\n//                  | \\_|  ''\\---/''  |   |                       //\n//                  \\  .-\\__  `-`  ___/-. /                       //\n//                ___`. .'  /--.--\\  `. . ___                     //\n//              .\"\" '<  `.___\\_<|>_/___.'  >'\"\".                  //\n//            | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |                 //\n//            \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /                 //\n//      ========`-.____`-.___\\_____/___.-`____.-'========         //\n//                           `=---='                              //\n//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //\n//         佛祖保佑       永无BUG     永不修改                        //\n////////////////////////////////////////////////////////////////////");
}

@end
